Transform Weather Sensor Data from Waggle to an Xarray Dataset + Plot with ACT#
Imports#
import sage_data_client
from bokeh.models.formatters import DatetimeTickFormatter
import hvplot.pandas
import hvplot.xarray
import holoviews as hv
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import xarray as xr
import matplotlib.pyplot as plt
from metpy.plots import USCOUNTIES
import act
import numpy as np
import pandas as pd
import warnings
from bokeh.models import DatetimeTickFormatter
def apply_formatter(plot, element):
plot.handles['xaxis'].formatter = DatetimeTickFormatter(hours='%m/%d/%Y \n %H:%M',
minutes='%m/%d/%Y \n %H:%M',
hourmin='%m/%d/%Y \n %H:%M',
days='%m/%d/%Y \n %H:%M',
months='%m/%d/%Y \n %H:%M')
xr.set_options(keep_attrs=True)
warnings.filterwarnings("ignore")
hv.extension("bokeh")
Query for the available Data#
wxt_df = sage_data_client.query(
start="-3h",
filter={
"sensor": "vaisala-wxt536"
}
)
Configure Helper Functions and Renaming Conventions#
The renaming is required due to . notations being problematic when working with both Pandas and Xarray data structures.
variable_rename_dict = {'wxt.env.humidity':'air_humidity',
'wxt.env.pressure':'air_pressure',
'wxt.env.temp':'air_temperature',
'wxt.heater.temp':'heater_temperature',
'wxt.heater.volt':'heater_voltage',
'wxt.rain.accumulation':'rain_accumulation',
'wxt.wind.direction':'wind_direction',
'wxt.wind.speed':'wind_speed',
'sys.gps.lat':'latitude',
'sys.gps.lon':'longitude',
}
def generate_data_array(df, variable, rename_variable_dict=variable_rename_dict):
new_variable_name = rename_variable_dict[variable]
df_variable= df.loc[df.name == variable]
ds = df_variable.to_xarray().rename({'value':new_variable_name,
'timestamp':'time',
'meta.vsn':'node'})
ds[new_variable_name].attrs['units'] = df_variable['meta.units'].values[0]
ds['time'] = pd.to_datetime(ds.time)
ds.attrs['datastream'] = ds.node.values[0]
return ds[[new_variable_name]]
def generate_dataset(df, variables, rename_variable_dict=variable_rename_dict):
reindexed = df.set_index(['meta.vsn', 'timestamp'])
return xr.merge([generate_data_array(reindexed, variable) for variable in variables])
Transform the Data to Xarray#
wxt_variables = wxt_df.name.unique()
wxt_variables
array(['wxt.env.humidity', 'wxt.env.pressure', 'wxt.env.temp',
'wxt.heater.temp', 'wxt.heater.volt', 'wxt.rain.accumulation',
'wxt.wind.direction', 'wxt.wind.speed'], dtype=object)
wxt_ds = generate_dataset(wxt_df, wxt_variables).squeeze()
wxt_ds
<xarray.Dataset> Size: 85MB
Dimensions: (node: 4, time: 321547)
Coordinates:
* node (node) object 32B 'W01B' 'W067' 'W08D' 'W099'
* time (time) datetime64[ns] 3MB 2024-04-10T13:05:11.5758620...
Data variables:
air_humidity (node, time) float64 10MB nan 91.4 nan ... nan 62.4 nan
air_pressure (node, time) float64 10MB nan 997.4 nan ... 990.9 nan
air_temperature (node, time) float64 10MB nan 8.6 nan ... nan 12.1 nan
heater_temperature (node, time) float64 10MB nan 12.9 nan ... nan 20.3 nan
heater_voltage (node, time) float64 10MB nan 11.4 nan ... nan 23.9 nan
rain_accumulation (node, time) float64 10MB nan 342.1 nan ... 311.0 nan
wind_direction (node, time) float64 10MB nan 314.0 nan ... 130.0 nan
wind_speed (node, time) float64 10MB nan 1.2 nan ... nan 1.8 nan
Attributes:
datastream: W01BResample the data to minute requency#
minute_ds = wxt_ds.resample(time='1T').mean()
Visualize using hvplot#
meteogram_variables = ['air_temperature', 'air_humidity', 'wind_speed', 'wind_direction']
plots = []
for variable in meteogram_variables:
plots.append(wxt_ds[variable].hvplot.line(label='10 Hz Data') *
minute_ds[variable].hvplot.line(label='1 Minute Data'))
hv.Layout(plots).cols(2)